﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gfw.BLL.Basic.SystemUser;
using Gfw.Models.Admin;
using Gfw.Repository.Admin;
using Gfw.Web.Common;
using Gfw.Web.Models.SystemUser;

namespace Gfw.Web.Areas.Admin.Controllers
{
    [LAuthorize(RequireRole = "Admin")]
    public class SystemUserController : Controller
    {
        SystemUserRepository _repository = new SystemUserRepository();

        #region 查询
        [OutputCache(Duration=1)]
        public ActionResult Index(int? page, string did)
        {
            ViewBag.page = page ?? 1;
            if (did != null)
                Params.DepartmentId = did;
            var models = Gfw.BLL.Basic.SystemUser.SystemUserLogic.QuerySystemUser(Params.DepartmentId);
            return PartialView(models);
        }

        #endregion

        #region 创建用户
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            ViewBag.Done = false;
            NewUserModel model = new NewUserModel();
            model.UserRoles = Gfw.BLL.Basic.SystemUser.SystemUserLogic.GetUserRoles();
            return PartialView(model);
        }

        [HttpPost]
        public PartialViewResult Create(NewUserModel user)
        {

            user.Id = Guid.NewGuid().ToString();
            if (ModelState.IsValid)
            {
                try
                {
                  
                    Gfw.BLL.Basic.SystemUser.SystemUserLogic.SaveNewSystemUser(user, Params.DepartmentId);
                    ViewBag.Done = true;
                }
                catch (Exception exp)
                {
                    ViewBag.Done = false;
                    ModelState.AddModelError("", exp.Message);

                }
            }
            return PartialView(user);
        }
        #endregion

        #region 删除用户
        [HttpPost]
        public ActionResult Delete(string id)
        {
            _repository.Delete(id);
            return RedirectToAction("Index", new { page = 1 });
        }

        #endregion

        #region 修改用户
        [OutputCache(Duration=1)]
        public ActionResult Edit(string id)
        {
            ViewBag.Done = false;
            var model = SystemUserLogic.GetUserModel(id);
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(NewUserModel user)
        {
           
            if (ModelState.IsValid)
            {
                try
                {

                    Gfw.BLL.Basic.SystemUser.SystemUserLogic.EditSystemUser(user);
                    ViewBag.Done = true;
                }
                catch (Exception exp)
                {
                    ViewBag.Done = false;
                    ModelState.AddModelError("", exp.Message);

                }
            }
            return PartialView(user);

        }

        #endregion

    }
}
